package com.sjx.boundarycallback.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;


import com.sjx.boundarycallback.model.PieChartBean;

import java.util.ArrayList;

public class ArcCircleView extends View {
    private ArrayList<PieChartBean> mPieChartList = new ArrayList<>();
    private Paint mArcPaint;
    private Paint mRectBlackPaint;
    private Paint mRectWhitePaint;
    private int mRadius;
    private float mCircleStrokeWidth;
    private RectF mRectF = new RectF();
    private int mCircleX;
    private int mCircleY;
    private float mAnimationAngle;
    private ValueAnimator animation;
    public ArcCircleView(Context context) {
        this(context, null);
    }

    public ArcCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    private void init() {
        mArcPaint = new Paint();
        mRectBlackPaint = new Paint();
        mRectWhitePaint = new Paint();
        mArcPaint.setColor(Color.RED);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mRectBlackPaint.setColor(Color.BLACK);
        mRectBlackPaint.setStyle(Paint.Style.STROKE);
        mRectBlackPaint.setStrokeWidth(50);
        mRectWhitePaint.setColor(Color.WHITE);
        mRectWhitePaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRadius = Math.min(w, h) / 2;
        mCircleStrokeWidth = mRadius * 0.3f;
        mArcPaint.setStrokeWidth(mCircleStrokeWidth);
        float rectSide = mRadius - mCircleStrokeWidth / 2;
        mCircleX = w /2;
        mCircleY = h /2;
        mRectF.set(-rectSide, -rectSide, rectSide, rectSide);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(mCircleX, mCircleY);
        float startAngle = 0;
        float drawAngle = 0;
        for (PieChartBean item: mPieChartList) {
            mArcPaint.setColor(item.getColor());
            drawAngle = Math.min(item.getPercentage(), mAnimationAngle - startAngle);
            if (drawAngle > 0) {
                canvas.drawArc(mRectF, startAngle, drawAngle, false, mArcPaint);
            }
            startAngle += item.getPercentage();
        }

        canvas.restore();
    }

    public void setData(ArrayList arcColorCircleData) {
        mPieChartList.clear();
        if (arcColorCircleData != null) {
            mPieChartList.addAll(arcColorCircleData);
        }
    }

    public void setProgressAnimation(int duration) {
        if (animation != null && animation.isRunning()) {
            animation.cancel();
        } else {
            animation = ValueAnimator.ofFloat(0, 360).setDuration(duration);
            animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    mAnimationAngle = (float) animation.getAnimatedValue();
                    invalidate();
                }
            });
        }
        animation.start();
    }
}
